package com.jiaertangshi.jiaer.tools;



import java.io.IOException;


import net.sf.json.JSONException;
import net.sf.json.JSONObject;
import org.apache.commons.io.IOUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;  
/* 
 * 利用HttpClient进行post请求的工具类 
 */  
public class HttpClientUtil {  
	
	

    
    public JSONObject requestByGetMethod(String url,String  headName,String  headValue){
        //创建默认的httpClient实例
        CloseableHttpClient httpClient = getHttpClient();
        String  result=null;
        JSONObject   js1=null;
        try {
            //用get方法发送http请求
            HttpGet get = new HttpGet(url);
            get.setHeader(headName, headValue);
            System.out.println("执行get请求:...."+get.getURI());
            CloseableHttpResponse httpResponse = null;
            //发送get请求
            httpResponse = httpClient.execute(get);
            try{
                //response实体
                HttpEntity resEntity = httpResponse.getEntity();
                if(resEntity != null){  
                   result = EntityUtils.toString(resEntity,"utf-8");  
                	js1=	new  JSONObject().fromObject(result);
                }
                if (null != resEntity){
                    System.out.println("响应状态码:"+ httpResponse.getStatusLine());
                    System.out.println("-------------------------------------------------");
                    System.out.println("响应内容:" + EntityUtils.toString(resEntity));
                    System.out.println("-------------------------------------------------");                    
                }
            }
            finally{
                httpResponse.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        finally{
            try{
                closeHttpClient(httpClient);
            } catch (IOException e){
                e.printStackTrace();
            }
        }
        return   js1;
    }
    
    
    
    public JSONObject doGETSSL(String url,String charset,Object json,String  headName,String  headValue){  
        HttpClient httpClient = null;  
        HttpGet httpget = null;  
        String result = null;  
        HttpResponse response=null;
        JSONObject   js1=new   JSONObject();
        try{  
            httpClient = new SSLClient();  
            httpget = new HttpGet(url);  
            httpget.setHeader(headName, headValue);
            response = httpClient.execute(httpget); 
            int statusCode = response.getStatusLine().getStatusCode();  
            System.out.println(statusCode+"statusCode");
            if(response != null){
                	 result = IOUtils.toString(response.getEntity().getContent(), "utf-8");
                	 System.out.println(result+"result");
                	 js1.put("result", result);
            }else{
            	result="请求失败";
            }
        }catch(Exception ex){  
            ex.printStackTrace();  
        }  
        return  js1;
    } 
    
    
    
    private CloseableHttpClient getHttpClient(){
        return HttpClients.createDefault();
    }
     
    
    private void closeHttpClient(CloseableHttpClient client) throws IOException{
        if (client != null){
            client.close();
        }
    }
    
}  